package com.web.base.open.util;

import com.blue.jdbc.Page;
import com.web.base.open.message.AccountRequest;
import com.web.base.open.message.AccountResponse;
import com.web.base.open.model.Account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开放平台帐号转换器
 *
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年02月27日
 */
public class AccountConvertor
{
	private AccountConvertor()
	{
	}

	public static Account toAccount(AccountRequest request)
	{
		if (request == null)
			return null;

		Account account = new Account();
		account.setId(request.getId());
		account.setName(request.getName());
		account.setSecret(request.getSecret());
		account.setDescp(request.getDescp());
		account.setCreateTime(request.getCreateTime());
		account.setState(request.getState());
		return account;
	}

	public static AccountResponse toResponse(Account account)
	{
		if (account == null)
			return null;

		AccountResponse response = new AccountResponse();
		response.setId(account.getId());
		response.setName(account.getName());
		response.setSecret(account.getSecret());
		response.setDescp(account.getDescp());
		response.setCreateTime(account.getCreateTime());
		response.setState(account.getState());
		return response;
	}

	public static List<AccountResponse> toResponse(List<Account> list)
	{
		List<AccountResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;

		for (Account account : list)
		{
			responseList.add(toResponse(account));
		}
		return responseList;
	}

	public static Map<Integer, AccountResponse> toResponse(Map<Integer, Account> map)
	{
		Map<Integer, AccountResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;

		for (Map.Entry<Integer, Account> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}

	public static Page<AccountResponse> toResponse(Page<Account> page)
	{
		if (page == null)
			return null;

		Page<AccountResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}

}
